/* Directives */
var appDict = angular.module("iDirective", []);

//搜索
appDict.directive('searchBox',['$http','$location',function ($http,$location) {
    return {
        restrict: 'EA',
        link : function ($scope, elem) {
            //筛选按钮
            var speed = 300, wacth1,wacth2,
                choiceBtn = elem.find('.choice'),
                alert_bg = elem.find('.alert-bg'),
                _alert = elem.find('.alert'),
                input = elem.find('.number input');
            choiceBtn.on('click',function () {
                alert_bg.fadeIn(speed,'linear');
                _alert.animate({right:0},speed,'linear');
            });
            alert_bg.on('click',function () {
                $(this).fadeOut(speed,'linear');
                _alert.animate({right:'-100%'},speed,'linear');
            });
            //获得焦点去除等级选择
            input.on('focus',function () {
                $scope.$apply(function () {
                    $scope.search.grade.type = '';
                })
            })
            //获取类型
            $http.get($config.host+'?m=Policy&a=searchCondition')
                .success(function (json) {
                    if(json.errcode==0){
                        $scope.namelist = json.data;
                    }
                })

            //初始化数据
            $scope.search = {
                industry:'',
                type:'',
                min:'',
                max:'',
                grade: {
                    type:'',
                },
                reset : function () {
                    this.industry = '';
                    this.type = '';
                    this.min = '';
                    this.max = '';
                    this.grade.type = '';
                },
                choice :  {
                    industry : function (value) {
                        $scope.search.industry = value;
                    },
                    type : function (value) {
                        $scope.search.type = value;
                    },
                    grade : function (min,max,index) {
                        $scope.search.grade.type = index;
                        $scope.search.grade.min = min;
                        $scope.search.grade.max = max;
                        $scope.search.min = '';
                        $scope.search.max = '';
                    }
                },
                confirm : function () {
                    //输入字符验证
                    // if(!$scope.search.grade.type && !searchVerify()) return;
                    var postData = {
                        name : 'filter',
                        industry : $scope.search.industry,
                        type : $scope.search.type
                    };

                    if(!$scope.search.grade.type){
                        postData.min = $scope.search.min;
                        postData.max = $scope.search.max;
                    }else{
                        postData.gradetype = $scope.search.grade.type;
                        postData.min = $scope.search.grade.min;
                        postData.max = $scope.search.grade.max;
                    }

                    console.log(postData);

                    //跳转
                    if($location.path()=='/result'){
                        $location.replace('/result').search(postData);
                        setTimeout(function () {
                            location.reload();
                        },100);
                    }else{
                        $location.path('/result').search(postData);
                    }
                },
                result : {
                    name : '',
                    btn : function () {
                        if(!$scope.search.result.name){
                            $scope.showPrompt('请输入搜索关键字！');
                            return;
                        }
                        var postData = {
                            name : 'sou',
                            cont : $scope.search.result.name,
                        };
                        console.log(postData);
                        //跳转
                        if($location.path()=='/result'){
                            $location.replace('/result').search(postData);
                            setTimeout(function () {
                                location.reload();
                            },100);
                        }else{
                            $location.path('/result').search(postData);
                        }

                    }
                }
            }

            //筛选页面设置筛选选项
            var searchs = $location.search();
            if(searchs.name=='filter'){
                if(searchs.type){
                    $scope.search.type = searchs.type;
                }
                if(searchs.industry){
                    $scope.search.industry = searchs.industry;
                }
                if(searchs.gradetype){
                    $scope.search.grade.type = searchs.gradetype;
                    $scope.search.grade.min = searchs.min;
                    $scope.search.grade.max = searchs.max;
                }else{
                    $scope.search.min = searchs.min;
                    $scope.search.max = searchs.max;
                }
            }


            //填写分数验证
            function searchVerify() {
                if(!$scope.search.type&&!$scope.search.industry&&!$scope.search.min&&!$scope.search.max&&!$scope.search.grade.type){
                    $scope.showPrompt('请选择或输入筛选条件');
                    return false;
                }else if(isNaN(Number($scope.search.min))||isNaN(Number($scope.search.max))){
                    $scope.showPrompt('分数必须输入数字！');
                    return false;
                }else if($scope.search.min&&!$scope.search.max){
                    $scope.showPrompt('请选择最高分数！');
                    return false;
                }else if(!$scope.search.min&&$scope.search.max){
                    $scope.showPrompt('请选择最低分数！');
                    return false;
                }else if(!$scope.search.min&&$scope.search.max){
                    $scope.showPrompt('请选择最低分数！');
                    return false;
                }else if($scope.search.min>$scope.search.max){
                    $scope.showPrompt('最低分数不能大于最高分数！');
                    return false;
                }else {
                    return true;
                }
            }
        }
    }
}])

//分享弹出框
appDict.directive('shareAlert',['$timeout',function ($timeout) {
    return {
        restrict: 'EA',
        // template:'<div class="share-box animated fadeIn" ng-if="isShareAlert" ng-click="shareShow()"> <img src="'+__uri('/src/img/share-icon.png')+'" alt=""></div><div class="prompt animated tada" ng-show="prompt"><p ng-bind-html="promptTxt | trustHtml"></p></div>',
        templateUrl: 'views/template/share-alert.html',
        link : function ($scope, elem) {
            // 分享提示
            $scope.shareShow = function (boole) {
                if(boole){
                    $scope.isShareAlert = true;
                }else{
                    $scope.isShareAlert = false;
                }

            }
            // 显示弹出框
            $scope.showPrompt = function (value,time,func) {
                if(time){
                    var speed = time;
                }else {
                    var speed = 3000;
                }
                $scope.prompt = true;
                $scope.promptTxt = value;
                $timeout(function () {
                    $scope.prompt = false;
                    if(func){
                        func();
                    }
                },speed)
            }
        }
    }
}])